package com.hyj.shop.project.index.service.impl;

import com.hyj.shop.project.index.domain.Article;
import com.hyj.shop.project.index.domain.Comment;
import com.hyj.shop.project.index.domain.HomeBackground;
import com.hyj.shop.project.index.mapper.IndexMapper;
import com.hyj.shop.project.index.mapper.SwipperMapper;
import com.hyj.shop.project.index.service.IndexService;
import com.hyj.shop.project.utils.domain.AjaxResult;
import com.hyj.shop.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexMapper indexMapper;


    @Override
    public List<Article> queryArticleByPage(Article article) {
        return indexMapper.queryByPage(article);
    }

    @Override
    public AjaxResult submitComment(Map map) {
        String date =DateUtils.getTime();
        map.put("createTime",date);
        if(map.get("responsePersonId")==""){
            //包含子评论
            System.out.println("老子现在不等于null");
            System.out.println();
        }
        indexMapper.submitComment(map);

       AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    @Override
    public AjaxResult queryComment(int articleId) {
        List list = indexMapper.queryComment(articleId);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("list",list);
        return ajaxResult;
    }

    @Override
    public AjaxResult publishArticle(Article article) {
        try{
            String now = DateUtils.getTime();
            Date  date = DateUtils.parseDate(now);
            article.setCreateTime(date);
            indexMapper.publishArticle(article);
            AjaxResult ajaxResult =AjaxResult.success();
            return ajaxResult;
        }catch (Exception e){
            e.printStackTrace();
            AjaxResult ajaxResult =AjaxResult.error();
            return ajaxResult;
        }

    }

    @Override
    public AjaxResult updateArticle(Article article) { // 文章修改
        try{
            String now = DateUtils.getTime();
            Date date = DateUtils.parseDate(now);
            System.out.println("现在的修改时间");
            System.out.println(now);
            article.setUpdateTime(date);
            indexMapper.updateArticle(article);
            AjaxResult ajaxResult =AjaxResult.success("更新成功");
            return ajaxResult;
        }catch (Exception e){
            e.printStackTrace();
            AjaxResult ajaxResult =AjaxResult.error("更新失败");
            return ajaxResult;
        }
}

    @Override
    public AjaxResult articleDetail(int articleId) {
        Article article = indexMapper.articleDetail(articleId);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("data",article);
        return ajaxResult;
    }

    @Override
    public AjaxResult getHomeBackground() {
        List<HomeBackground> list = indexMapper.getHomeBackground();
        AjaxResult ajaxResult =AjaxResult.success();
        ajaxResult.put("data",list);
        return ajaxResult;
    }
}
